package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianStoreClient;
import com.diandian.backend.entrance.client.dto.OfflineStoreBatchAddParamDTO;
import com.diandian.backend.entrance.client.dto.OfflineStoreOpenApiPageParamDTO;
import com.diandian.backend.entrance.client.dto.OfflineStoreQueryAllStoreCodeParamDTO;
import com.diandian.backend.entrance.client.dto.StoreDouyinLifeStoreInfoListParamDTO;
import com.diandian.backend.entrance.client.dto.store.StoreDouyinLifeStoreInfoDetailByCodeParamDTO;
import com.diandian.backend.entrance.client.dto.store.StoreDouyinLifeStoreInfoDetailByRoomIdParamDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterStoreService {

    private static final Logger logger = LoggerFactory.getLogger(RouterStoreService.class);

    @Autowired
    private DianDianStoreClient dianDianStoreClient;

    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 线下门店-分页查询门店数据
        if(EntranceConstant.ROUTER_STORE_QUERY_PAGE.equals(cmd)){

            logger.info("执行分页查询门店数据请求，参数: {}", data.toJSONString());

            // 参数转换
            OfflineStoreOpenApiPageParamDTO paramDTO = JSONObject.parseObject(data.toString(), OfflineStoreOpenApiPageParamDTO.class);

            // 补充参数
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }
            paramDTO.setBelongEql(belong);

            return dianDianStoreClient.getStorePage(paramDTO);

        }

        // 线下门店-查询所有的门店编码
        if(EntranceConstant.ROUTER_STORE_QUERY_CODE_LIST.equals(cmd)){

            // 参数转换
            OfflineStoreQueryAllStoreCodeParamDTO paramDTO = JSONObject.parseObject(data.toString(), OfflineStoreQueryAllStoreCodeParamDTO.class);

            // 补充参数
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }
            paramDTO.setBelong(belong);

            return dianDianStoreClient.getAllStoreCodeList(paramDTO);

        }

        // 线下门店-批量同步门店数据
        if(EntranceConstant.ROUTER_STORE_ADD_BATCH.equals(cmd)){

            // 参数转换
            OfflineStoreBatchAddParamDTO paramDTO = JSONObject.parseObject(data.toString(), OfflineStoreBatchAddParamDTO.class);

            // 补充参数
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }
            paramDTO.setBelong(belong);

            return dianDianStoreClient.batchSyncStore(paramDTO);
        }

        // 抖音本地生活门店-根据线下或线上门店编码查询门店信息
        if(EntranceConstant.ROUTER_STORE_DOUYIN_LIFE_QUERY_BY_CODE.equals(cmd)){

            // 参数转换
            StoreDouyinLifeStoreInfoDetailByCodeParamDTO paramDTO = JSONObject.parseObject(data.toString(), StoreDouyinLifeStoreInfoDetailByCodeParamDTO.class);

            // 补充参数
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }
            paramDTO.setBelong(belong);

            return dianDianStoreClient.getDetailByCode(paramDTO);
        }

        // 抖音本地生活门店-根据直播间id查询门店信息
        if(EntranceConstant.ROUTER_STORE_DOUYIN_LIFE_QUERY_BY_ROOM_ID.equals(cmd)){

            // 参数转换
            StoreDouyinLifeStoreInfoDetailByRoomIdParamDTO paramDTO = JSONObject.parseObject(data.toString(), StoreDouyinLifeStoreInfoDetailByRoomIdParamDTO.class);

            // 补充参数
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }
            paramDTO.setBelong(belong);

            return dianDianStoreClient.getDetailByRoomId(paramDTO);
        }

        // 抖音本地生活门店-获取门店信息列表
        if(EntranceConstant.ROUTER_STORE_DOUYIN_LIFE_QUERY_LIST.equals(cmd)){

            // 补充参数
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            StoreDouyinLifeStoreInfoListParamDTO listParamDTO = new StoreDouyinLifeStoreInfoListParamDTO();
            listParamDTO.setBelongEql(belong);

            return dianDianStoreClient.getDouYinStoreList(listParamDTO);
        }

        throw new DefaultException("暂不支持此操作");
    }

}
